<template>
  <div class="articles-page">
    <div class="search-container">
      <h2 class="section-title">文章列表</h2>
      <div class="search-box">
        <i class="fas fa-search"></i>
        <input 
          type="text" 
          v-model="searchQuery" 
          placeholder="搜索文章标题..." 
          @input="handleSearch"
        >
      </div>
    </div>
    <transition name="fade" mode="out-in">
      <div v-if="!selectedArticle" class="articles-container">
        <div class="blog-card" 
             v-for="post in filteredPosts" 
             :key="post.id"
             @click="showArticleDetail(post)">
          <div class="card-img" :style="{ background: post.color }"></div>
          <div class="card-content">
            <span class="card-tag">{{ post.tag }}</span>
            <h3 class="card-title">{{ post.title }}</h3>
            <p class="card-excerpt">{{ post.excerpt }}</p>
            <div class="card-meta">
              <span><i class="far fa-calendar"></i> {{ post.date }}</span>
              <span><i class="far fa-clock"></i> {{ post.readTime }} 分钟阅读</span>
            </div>
          </div>
        </div>
      </div>
      <article-detail
        v-else
        :article="selectedArticle"
        @back="selectedArticle = null"
        @like="handleLike"
      />
    </transition>
  </div>
</template>

<script>
import ArticleDetail from './ArticleDetail.vue'

export default {
  name: 'Articles',
  components: {
    ArticleDetail
  },
  props: {
    posts: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
      searchQuery: '',
      filteredPosts: [],
      selectedArticle: null
    }
  },
  watch: {
    posts: {
      immediate: true,
      handler(newPosts) {
        this.filteredPosts = [...newPosts]
      }
    }
  },
  methods: {
    handlePostClick(post) {
      this.$emit('post-click', post)
    },
    handleSearch() {
      if (!this.searchQuery.trim()) {
        this.filteredPosts = [...this.posts]
        return
      }
      const query = this.searchQuery.toLowerCase().trim()
      this.filteredPosts = this.posts.filter(post => 
        post.title.toLowerCase().includes(query)
      )
    },
    showArticleDetail(post) {
      // 模拟获取文章详细内容
      const articleWithContent = {
        ...post,
        content: `
          <h2>${post.title}</h2>
          <p>${post.excerpt}</p>
          <h3>文章内容</h3>
          <p>这是一篇关于${post.tag}的详细文章。在这里，我们将深入探讨相关主题，分享实用的技巧和最佳实践。</p>
          <h3>主要内容</h3>
          <p>文章将包含以下主要内容：</p>
          <ul>
            <li>核心概念解析</li>
            <li>实际应用案例</li>
            <li>最佳实践建议</li>
            <li>常见问题解答</li>
          </ul>
          <h3>代码示例</h3>
          <pre><code>// 示例代码
function example() {
  console.log('Hello, World!');
}</code></pre>
          <p>更多详细内容将在后续更新...</p>
        `,
        tags: [post.tag, '教程', '技术分享', '最佳实践'],
        likes: Math.floor(Math.random() * 100)
      }
      this.selectedArticle = articleWithContent
    },
    handleLike(articleId) {
      // 实现点赞功能
      console.log('Article liked:', articleId)
    }
  }
}
</script>

<style scoped>
.articles-page {
  padding: 20px 0;
}

.section-title {
  font-size: 2rem;
  margin-bottom: 30px;
  position: relative;
  padding-bottom: 15px;
  color: var(--dark);
  padding-left: 15px;
}

.section-title:after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 15px;
  width: 60px;
  height: 4px;
  background: var(--primary);
  border-radius: 2px;
}

.articles-container {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(350px, 1fr));
  gap: 30px;
  padding: 0 15px;
  column-gap: 30px;
}

.blog-card {
  background: var(--card-bg);
  border-radius: 24px;
  overflow: hidden;
  transition: var(--transition);
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.05);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.5);
  cursor: pointer;
  break-inside: avoid;
  margin-bottom: 30px;
  display: inline-block;
  width: 100%;
  position: relative;
}

.blog-card:hover {
  transform: translateY(-10px);
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
}

.card-img {
  width: 100%;
  height: 200px;
  object-fit: cover;
}

.card-content {
  padding: 20px;
}

.card-tag {
  display: inline-block;
  background: var(--primary-light);
  color: white;
  padding: 5px 15px;
  border-radius: 50px;
  font-size: 0.8rem;
  margin-bottom: 15px;
}

.card-title {
  font-size: 1.4rem;
  margin-bottom: 12px;
  color: var(--dark);
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  line-height: 1.4;
}

.card-excerpt {
  color: var(--gray);
  margin-bottom: 20px;
  line-height: 1.6;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.card-meta {
  display: flex;
  justify-content: space-between;
  color: var(--gray);
  font-size: 0.9rem;
}

.card-meta i {
  margin-right: 5px;
}

.search-container {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30px;
  padding: 0 15px;
  flex-wrap: wrap;
  gap: 15px;
}

.search-box {
  position: relative;
  flex: 1;
  max-width: 300px;
}

.search-box i {
  position: absolute;
  left: 12px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--gray);
}

.search-box input {
  width: 100%;
  padding: 10px 12px 10px 35px;
  border: 1px solid rgba(0, 0, 0, 0.1);
  border-radius: 50px;
  font-size: 0.9rem;
  background: var(--card-bg);
  backdrop-filter: blur(10px);
  transition: var(--transition);
}

.search-box input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.1);
}

@media (max-width: 900px) {
  .articles-container {
    column-count: 2;
    column-gap: 15px;
    padding: 0 10px;
    display: block;
  }

  .blog-card {
    margin-bottom: 15px;
    display: inline-block;
    width: 100%;
  }

  .section-title {
    font-size: 1.8rem;
    padding-left: 10px;
  }

  .section-title:after {
    left: 10px;
  }

  .card-content {
    padding: 15px;
  }

  .card-title {
    font-size: 1.2rem;
  }

  .card-excerpt {
    -webkit-line-clamp: 2;
    font-size: 0.9rem;
  }

  .card-meta {
    font-size: 0.8rem;
  }

  .search-container {
    padding: 0 10px;
    gap: 10px;
  }

  .search-box {
    max-width: 250px;
  }

  .search-box input {
    padding: 8px 12px 8px 32px;
    font-size: 0.85rem;
  }
}

@media (max-width: 600px) {
  .articles-page {
    padding: 10px 0;
  }

  .articles-container {
    column-count: 2;
    column-gap: 8px;
    padding: 0 2px;
    display: block;
  }

  .section-title {
    font-size: 1.5rem;
    margin-bottom: 15px;
    padding-left: 2px;
  }

  .section-title:after {
    left: 2px;
    width: 40px;
  }

  .blog-card {
    margin-bottom: 8px;
    border-radius: 12px;
    display: inline-block;
    width: 100%;
  }

  .card-img {
    height: 120px;
  }

  .card-content {
    padding: 8px;
  }

  .card-tag {
    padding: 2px 6px;
    font-size: 0.6rem;
    margin-bottom: 6px;
  }

  .card-title {
    font-size: 0.85rem;
    margin-bottom: 4px;
    -webkit-line-clamp: 2;
    line-height: 1.3;
  }

  .card-excerpt {
    margin-bottom: 6px;
    font-size: 0.7rem;
    -webkit-line-clamp: 2;
    line-height: 1.4;
  }

  .card-meta {
    font-size: 0.6rem;
  }

  .card-meta i {
    margin-right: 2px;
  }

  .search-container {
    padding: 0 2px;
    gap: 8px;
    margin-bottom: 15px;
  }

  .search-box {
    max-width: 100%;
  }

  .search-box input {
    padding: 6px 10px 6px 30px;
    font-size: 0.8rem;
  }

  .blog-card::after {
    display: none;
  }
}

/* 添加过渡动画 */
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s ease;
}

.fade-enter,
.fade-leave-to {
  opacity: 0;
}

/* 添加单击提示 */
.blog-card::after {
  content: '点击查看文章';
  position: absolute;
  top: 10px;
  right: 10px;
  background: rgba(0, 0, 0, 0.6);
  color: white;
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 0.7rem;
  opacity: 0;
  transition: opacity 0.3s ease;
}

.blog-card:hover::after {
  opacity: 1;
}
</style> 